package com.zq.it.action.admin;

import java.util.List;

import javax.annotation.Resource;

import com.zq.it.action.BaseAction;
import com.zq.it.dao.common.PageConfig;
import com.zq.it.pojo.Student;
import com.zq.it.pojo.TClass;
import com.zq.it.pojo.User;
import com.zq.it.pojo.exam.data.ObjectData;
import com.zq.it.pojo.exam.data.StudentData;
import com.zq.it.service.admin.StudentService;
import com.zq.it.tool.Constant;
import com.zq.it.tool.EtyLsName;
import com.zq.it.tool.FileName;
import com.zq.it.tool.FileType;
import com.zq.it.tool.HeadTitle;

public class StudentAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	@Resource
	private StudentService studentService;
	private static int classId;
	private int[] ids;
	public void searchStudentList() {
		List list = studentService.getAllStudent();
		uploadList(EtyLsName.STU_LS.name(), list);
		print(toJSON(list).toString(), "UTF-8", "APPLICATION/JSON");
	}
	public void searchStudentById() {
		List list = studentService.getStudent(stuId);
		uploadList(EtyLsName.STU_LS.name(), list);
		print(toJSON(list).toString(), "UTF-8", "text/text");
	}
	public void modifyClassPo() {
		if (student.getTclass().getClassId() == 0)
			student.setTclass(null);
		studentService.renewalStudent(student);
		print("<script>parent.closeWindow('已修改！');</script>", "UTF-8",
				"text/html");
	}
	
	public String modifyStudent() {
		studentService.renewalStudent(student);
		this.getHttpSession().setAttribute(Constant.STUDENT, student);
		return stuInforPage();
	}
	
	
	public void addStudentPo() {
		if (student.getTclass().getClassId() == 0)
			student.setTclass(null);
		studentService.addStudent(student);
		print("<script>parent.closeWindow('已添加');</script>", "UTF-8",
				"text/html");
	}
	public void searchClassNameAndId() {
		print(toJSON(studentService.getTclassNameAndId()).toString(), "UTF-8",
				"APPLICATION/JSON");
	}

	public void searchStuListFromType() {
		List list = studentService.getStudentListByType(type, param);
		uploadList(EtyLsName.STU_LS.name(), list);
		print(toJSON(list).toString(), "UTF-8", "APPLICATION/JSON");
	}
	public void importStuList() {
		String msgStr = "";
		List<Student> list = studentService.importStudentList(getUpload());
		// if(list!=null){
		if (list.size() > 0) {
			getRequest().getSession().setAttribute("sexistList", list);
			msgStr = "existList";
		} else {
			msgStr = "已导入成功!";
		}
		print("<script>parent.importBackResult('" + msgStr
				+ "','webpage/adminpage/stu/sexistList.jsp');</script>",
				"UTF-8", "TEXT/HTML");
	}/*
		 * else{ print("<script>parent.info('请保证导入数据完整性',3);</script>","UTF-8",
		 * "TEXT/HTML"); }
		 */
	// }

	public void deleteStudentGroups() {
		studentService.removeAllStudent(stuGroupId);
		print("已删除" + stuGroupId.length + "条记录！", "UTF-8", "text/html");
	}
	
	public String exit() {
		getHttpSession().removeAttribute(Constant.USER);
		getHttpSession().removeAttribute(Constant.STUDENT);
		return INPUT;
	}
	
	public String getStudentByClass(){
		if(ids!=null){			
			classId = ids[0];
		}
		PageConfig config = new PageConfig();
		config.setHql("from Student as t where t.tclass.classId ="+classId);
		config.setCountHql("select count(*) from Student as t where t.tclass.classId ="+classId);
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setUrl("student_getStudentByClass.asp");
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		students = studentService.getListByPage(config);
		classList= classService.getAllClass();
		studentData = new StudentData(students.getList());
		getHttpSession().setAttribute(Constant.Student_IN_SESSION, studentData);		
		return "studentOfClass";
	}
	
	public void validateLogin() {
			if (student == null) {
				student = new Student();
				student.setStuId(Long.parseLong(this.getAccount()));
				student.setStuPassword(this.getPassword());
			}
			List list = studentService.getStudentByExmp(student);
			if (list.size() > 0) {
				student = (Student)list.get(0);
			} else {
				addFieldError("account", "用户不存在或密码不正确！");
			}	
	}
	
	public String login() {
			getHttpSession().setAttribute(Constant.STUDENT, student);
			getHttpSession().setAttribute(Constant.USER, new User(student.getStuId().toString()));
			return SUCCESS;
	}
	
	public void exportStuList() {
		download(
				studentService.exportStudentList(
						HeadTitle.CN_STUDENTHEAD.getArray(),
						(List) getHttpSession()
								.getAttribute(EtyLsName.STU_LS.name())),
				FileName.STUDENT_LIST_NAME.toString() + FileType.XLSX);
	}
	
	
	public String stuInforPage() {
		return "stuInfor";
	}
	
	public String changePswPage() {
		return "changePswPage";
	}

	public long[] getStuGroupId() {
		return stuGroupId;
	}
	public void setStuGroupId(long[] stuGroupId) {
		this.stuGroupId = stuGroupId;
	}

	public int getStuId() {
		return stuId;
	}

	public void setStuId(int stuId) {
		this.stuId = stuId;
	}

	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public String getParam() {
		return param;
	}
	public void setParam(String param) {
		this.param = param;
	}
	
	public static int getClassId() {
		return classId;
	}
	public static void setClassId(int classId) {
		StudentAction.classId = classId;
	}

	public int[] getIds() {
		return ids;
	}
	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public ObjectData getStudents() {
		return students;
	}
	public void setStudents(ObjectData students) {
		this.students = students;
	}
	public List<TClass> getClassList() {
		return classList;
	}
	public void setClassList(List<TClass> classList) {
		this.classList = classList;
	}

	public StudentData getStudentData() {
		return studentData;
	}
	public void setStudentData(StudentData studentData) {
		this.studentData = studentData;
	}

	private int stuId;
	private long[] stuGroupId;
	private Student student;
	private int type;
	private String param;
	private ObjectData students;
	private List<TClass> classList;
	private StudentData studentData;
}
